<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 
      有三种情况在vue2里面响应式会失效:
        1. 在对象里面挂载新属性
        2. 直接修改数组的长度
        3. 通过索引改变数组项
     -->
    <div id="app">
      <h2>vue2-深层响应式</h2>
      <p>{{ message }}</p>
      <p>obj: {{ obj }}</p>
      <button v-on:click="fn">btn</button>

      <p>arr: {{ arr }}</p>
      <button v-on:click="fn2">btn2</button>
      <button v-on:click="fn3">btn3</button>
    </div>

    <script src="./lib/Vue2.js"></script>
    <script>
      new Vue({
        data() {
          return {
            message: "Hello Vue!",
            obj: {
              name: "zhangsan",
              age: 20,
            },
            arr: [1, 2, 3, 4, 5],
          };
        },
        methods: {
          fn() {
            this.obj.sex = 1;
            this.obj = { ...this.obj };
          },
          fn2() {
            this.arr.length = 3;
            this.arr = [...this.arr];
          },
          fn3() {
            this.arr[1] = 20;
            // this.arr = [...this.arr];

            this.$forceUpdate();
          },
        },
      }).$mount("#app");
    </script>
  </body>
</html>
